<template>
  <view style="padding-bottom: 150rpx;">
    <view style="color: #999;padding: 30rpx;"><text style="color: red;">*</text>为了您的资金安全，请提交以下信息进行身份核实</view>
    <view class="wrap">

      <u-form :model="form" ref="uForm" :rules="rules">
        <u-form-item label-width="150" label="身份证号" prop="idCard">
          <u-input placeholder="请输入身份证号" v-model="form.idCard" type="text" maxlength="18"></u-input>
        </u-form-item>
        <u-form-item label-width="150" label="姓名" prop="realName">
          <u-input placeholder="请输入姓名" v-model="form.realName" type="text"></u-input>
        </u-form-item>
      </u-form>
      <view>
        <view style="padding: 20rpx 0;margin-bottom: 30rpx;" @click="uploadAuthImg('idcardPhoto')">
          <view style="padding: 20rpx 0 40rpx;">身份证正面照</view>
          <template v-if="form.idcardPhoto">
            <image :src="form.idcardPhoto" style="width: 70%;height: 300rpx;margin-left: 14%;"></image>
          </template>
          <template v-else>
            <view class="u-f u-f-jsb">
              <image src="../../static/images/huangjin/front.png" mode="widthFix" style="width: 300rpx;"></image>
              <view class="u-flex-1 u-f-ac u-f-column">
                <view style="font-weight: 700;margin: 20rpx 0;">请上传人像面</view>
                <view>需文字清晰，边框完整</view>
              </view>
            </view>
          </template>
        </view>
        <view style="padding: 20rpx 0;margin-bottom: 30rpx;" @click="uploadAuthImg('idcardBackPhoto')">
          <view style="padding: 20rpx 0 40rpx;">身份证反面照</view>
          <template v-if="form.idcardBackPhoto">
            <image :src="form.idcardBackPhoto" style="width: 70%;height: 300rpx;margin-left: 14%;"></image>
          </template>
          <template v-else>
            <view class="u-f u-f-jsb">
              <image src="../../static/images/huangjin/back.png" mode="widthFix" style="width: 300rpx;"></image>
              <view class="u-flex-1 u-f-ac u-f-column">
                <view style="font-weight: 700;margin: 20rpx 0;">请上传国徽面</view>
                <view>需文字清晰，边框完整</view>
              </view>
            </view>
          </template>
        </view>
        <view style="padding: 20rpx 0;margin-bottom: 30rpx;" @click="uploadAuthImg('portrait')">
          <view style="padding: 20rpx 0 40rpx;">身份证正面照</view>
          <template v-if="form.portrait">
            <image :src="form.portrait" style="width: 70%;height: 300rpx;margin-left: 14%;"></image>
          </template>
          <template v-else>
            <view class="u-f u-f-jsb">
              <image src="../../static/images/huangjin/shouchi.png" mode="widthFix" style="width: 300rpx;"></image>
              <view class="u-flex-1 u-f-ac u-f-column">
                <view style="font-weight: 700;margin: 20rpx 0;">请上传手持照</view>
                <view>需文字清晰，边框完整</view>
              </view>
            </view>
          </template>
        </view>
      </view>
      <!-- <view class="sfz_item relative" style="margin-top: 30rpx;">
        <image :src="form.idcardPhoto || '/static/images/huangjin/front.png'" @tap="uploadAuthImg('idcardPhoto')" />
        <view style="margin-top: 10rpx;font-size: 555;">
          <u-icon name="arrow-upward" color="#09BB07" size="28"></u-icon>上传身份证正面
        </view>
      </view>
      <view class="sfz_item relative">
        <image :src="form.idcardBackPhoto || '/static/images/huangjin/back.png'" @tap="uploadAuthImg('idcardBackPhoto')" />
        <view style="margin-top: 10rpx;font-size: 555;">
          <u-icon name="arrow-upward" color="#09BB07" size="28"></u-icon>上传身份证背面
        </view>
      </view>
      <view class="sfz_item relative">
        <image :src="form.portrait || '/static/images/huangjin/shouchi.png'" @tap="uploadAuthImg('portrait')" />
        <view style="margin-top: 10rpx;font-size: 555;">
          <u-icon name="arrow-upward" color="#09BB07" size="28"></u-icon>上传手持证件
        </view>
      </view> -->

    </view>

    <u-button type="success" class="custom-style" @click="submit" shape="circle">{{btnText}}</u-button>

    <u-toast ref="uToast"></u-toast>
  </view>
</template>

<script>
  import {
    api
  } from '@/config/config.js'
  import * as API_Haibao from '@/api/haibao.js';
  export default {
    data() {
      return {
        btnText: '下一步',
        form: {
          realName: '',
          idCard: '',
          idcardPhoto: '',
          idcardBackPhoto: '',
          portrait: '',
        },
        rules: {
          realName: [{
              required: true,
              message: '请输入姓名',
              trigger: 'blur',
            },
            {
              min: 1,
              max: 8,
              message: '姓名长度在1到8个字符',
              trigger: ['change', 'blur'],
            },
          ],
          idCard: [{
              required: true,
              message: '请输入身份证号',
              trigger: ['change', 'blur'],
            }
          ],
        },
      }
    },
    onLoad() {

    },
    // 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
    onReady() {
      this.$refs.uForm.setRules(this.rules);
    },
    methods: {
      uploadAuthImg(names) {
        let that = this
        uni.chooseImage({
          count: 1,
          sizeType: ['original', 'compressed'],
          sourceType: ['album', 'camera'],
          success: res => {
            uni.showLoading({
              title: '上传中...'
            })
            const filePath = res.tempFilePaths[0]
            uni.uploadFile({
              url: `${api.base}/uploaders`,
              filePath,
              name: 'file',
              success: (uploadFileRes) => {
                const data = typeof uploadFileRes.data === 'string' ? JSON.parse(uploadFileRes.data) :
                  uploadFileRes.data
                // that.form.voucher = data.url.replace('localhost:7000', 'api.base.ruixingzhubao.xjkuang.com')
                that.form[names] = data.url
              },
              fail: () => {
                
              },
              complete: () => {
                uni.hideLoading()
              }
            })
          }
        })
      },
      
      submit() {
        if (!this.form.idcardPhoto || !this.form.idcardBackPhoto || !this.form.portrait) {
          return this.$u.toast('请上传所有身份证照')
        }
        this.$refs.uForm.validate(async valid => {
          if (valid) {
            console.log(this.form)
            uni.showLoading({
              title: '认证中...'
            })
            setTimeout(() => {
              uni.hideLoading()
            }, 1000)
            let res = await API_Haibao.putMemberIdentity(this.form)
            console.log(res)
            if (res) {
              this.$qqyUtils.uToast.call(this, '提交成功，请等待后台审核！', 'success')
              setTimeout(() => {
                this.$u.route({
                  type: 'tab',
                  url: `/pages/tabs/mine/mine`
                })
              }, 1200)
            } else {
              this.$qqyUtils.uToast.call(this, '提交成功！', 'error')
            }
          } else {
            console.log('验证失败');
          }
        });
      }
    }
  }
</script>

<style lang="scss">
  page {
    background-color: #F8F8F8;
  }

  .wrap {
    padding: 30rpx;
    background-color: #FFFFFF;
  }

  .custom-style {
    width: 90% !important;
    position: fixed !important;
    bottom: 50rpx !important;
    left: 5% !important;
  }

  .sfz_item {
    width: 80%;
    height: 320rpx;
    padding: 20rpx 0 0;
    margin: 0 0 80rpx 10%;
    background-color: #F1F8F9;
    border: 1px solid #09BB07;
    color: #09BB07;
    border-radius: 20rpx;
    text-align: center;

    image {
      width: 100%;
      height: 80%;
      border-radius: 20rpx;
    }

    .absolutes {
      width: 100%;
      position: absolute;
      top: 120rpx;
      text-align: center;
    }
  }
</style>
